package com.ruoyi.jacExcel.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jacExcel.mapper.SysUserMapper;
import com.ruoyi.jacExcel.service.ISysUserService;
import com.ruoyi.system.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysUserServiceImpl implements ISysUserService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper userMapper;


    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectUserList(SysUser user)
    {
        return userMapper.selectUserList(user);
    }

    @Override
    public String importFile(List<SysUser> userlist, Boolean isUpdateSupport) {
        if (StringUtils.isNull(userlist) || userlist.size() == 0)
        {
            throw new BusinessException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SysUser user: userlist)
        {
            try
            {
                // 验证是否存在这个计划
                List<SysUser> users = userMapper.selectUserList(user);
                if (users.size()==0)
                {
                    this.insertUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、用户 " + user.getUserName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    this.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、计划 " + user.getUserName() + " 更新成功");
                }
                else if(users.size()!=0)
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、计划： " + user.getUserName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、用户文件 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "成功条数:"+successNum+" ; 失败条数："+ failureNum + "; 以下数据导入失败：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int insertUser(SysUser user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUser(SysUser user) {
        return userMapper.updateUser(user);
    }

}
